package com.wyth.lottery.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wyth.lottery.R;
import com.wyth.lottery.adapter.ShoppingSSQAdapter;
import com.wyth.lottery.base.BaseActivity;
import com.wyth.lottery.base.SaveUtils;
import com.wyth.lottery.bean.RandomBean;
import com.wyth.lottery.bean.SSQDataBean;
import com.wyth.lottery.bean.SSQSelectBean;
<<<<<<< HEAD
=======
import com.wyth.lottery.bean.hezhi.HezhiBean;
>>>>>>> origin/master
import com.wyth.lottery.service.presenter.DetailPresenter;
import com.wyth.lottery.utils.BuildParamUtils;
import com.wyth.lottery.utils.MyApplication;
import com.wyth.lottery.utils.Number;
import com.wyth.lottery.utils.RandomUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by phh on 2017/10/31.
 * 购物车列表
 */

public class ShoppingActivity extends BaseActivity {


    @BindView(R.id.tv_number_bei)
    TextView mTvNumberBei;
    @BindView(R.id.select_delete)
    TextView mSelectDelete;
    @BindView(R.id.tv_next)
    TextView mTvNext;
<<<<<<< HEAD
    private DetailPresenter mDetailPresenter;
    private String detailInfo;
=======
>>>>>>> origin/master

    @BindView(R.id.type_ssq)
    TextView mTypeSsq;
    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.tv_money_number)
    TextView mTvMoneyNumber;
    @BindView(R.id.tv_money)
    TextView mTvMoney;
    /**
     * 用来区分用户从哪个界面过来的数据
     */
    private int intType;
    private String type;
    private ShoppingSSQAdapter mAdapter;

    @Override
    protected int getContentID() {
        return R.layout.activity_shopping;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initDate() {
        SSQDataBean stickyEvent = EventBus.getDefault().getStickyEvent(SSQDataBean.class);
        if (stickyEvent != null) {
            EventBus.getDefault().removeStickyEvent(stickyEvent);
        }
        RandomBean stickyEvent1 = EventBus.getDefault().getStickyEvent(RandomBean.class);
        if (stickyEvent1 != null) {
            EventBus.getDefault().removeStickyEvent(stickyEvent1);
        }
        HezhiBean stickyEvent2 = EventBus.getDefault().getStickyEvent(HezhiBean.class);
        if (stickyEvent2 != null) {
            EventBus.getDefault().removeStickyEvent(stickyEvent2);
        }

        String before = getIntent().getStringExtra("before");
        if (before != null) {
            mRv.setLayoutManager(new LinearLayoutManager(this));
            mAdapter = new ShoppingSSQAdapter(SaveUtils.getGoodsInfos().mlist, this);
            mRv.setAdapter(mAdapter);
            mTvMoneyNumber.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoneyNumber));
            mTvMoney.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoney));


        }
    }

    /**
     * 当用户选择的彩种数据从各个界面传递过来的时候在这个方法中进行二次拼接.
     *
     * @param event
     */
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Object event) {
        if (event instanceof SSQDataBean) {
            intType = ((SSQDataBean) event).getIntType();
            SetSsqData((SSQDataBean) event);
        }
        if (event instanceof RandomBean) {
            int type = ((RandomBean) event).getType();
            if (type == 1) {
                intType = 1;
                //说明用户是在双色球标准界面点击的随机
                int random = ((RandomBean) event).getRandom();
                if (random == 1) {
                    //表明用户点的是随机一注
                    initSSQJXYI();
                } else if (random == 5) {
                    initSSQJXWZ();
                } else if (random == 10) {
                    initSSQJXTEN();
                } else if (random == 20) {
                    mTvMoneyNumber.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoneyNumber));
                    mTvMoney.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoney));
                    mTvNumberBei.setText(SaveUtils.getGoodsInfos().mTvMoneyNumberBei+"");
                    if (SaveUtils.getGoodsInfos().mlist.size() == 0) {
                        mTvNext.setBackgroundResource(R.color.f850_default_blue);
                        mTvNext.setClickable(false);
                        mSelectDelete.setBackgroundResource(R.color.f850_default_blue);
                        mSelectDelete.setClickable(false);
                    }
                }
            }
        }
        if (event instanceof HezhiBean) {
            SetQThreeData((HezhiBean) event);
        }


    }

    private void SetQThreeData(HezhiBean event) {

        mTypeSsq.setText("快三");
        SaveUtils.getGoodsInfos().mThreelist.add(new HezhiBean(event.getmCunt(), event.getmType(), event.getmMoney(), event.getmStrName(), event.getmList()));
    }

    /**
     * 双色球机选十注
     */
    private void initSSQJXTEN() {
        for (int i = 0; i < 10; i++) {
            initSSQJXYI();
        }
    }

    /**
     * 双色球的机选五注
     */
    private void initSSQJXWZ() {
        for (int i = 0; i < 5; i++) {
            initSSQJXYI();
        }
    }

    /**
     * 双色球的机选一注
     */
    private void initSSQJXYI() {
        ArrayList<String> strings = RandomUtils.creatRandom(33, 6);
        List<SSQSelectBean> mRed = new ArrayList<>();
        List<Integer> mlist = new ArrayList<>();
        for (String string : strings) {
            int i = Integer.parseInt(string);
            mlist.add(i);
        }
        Collections.sort(mlist);
        for (Integer string : mlist) {
            if (string < 10) {
                String str = "0" + string;
                mRed.add(new SSQSelectBean(str, true, true));
            } else {
                mRed.add(new SSQSelectBean(String.valueOf(string), true, true));
            }
        }

        List<SSQSelectBean> mBlue = new ArrayList<>();
        ArrayList<String> strings1 = RandomUtils.creatRandom(16, 1);
        for (String s : strings1) {
            int i = Integer.parseInt(s);
            if (i < 10) {
                String str = "0" + s;
                mBlue.add(new SSQSelectBean(str, false, true));
            } else {
                mBlue.add(new SSQSelectBean(s, false, true));
            }
        }

        setAdapter(mRed, mBlue);
    }


    private void setAdapter(List<SSQSelectBean> mRed, List<SSQSelectBean> mBlue) {
        SaveUtils.getGoodsInfos().mlist.add(0,new SSQDataBean(mRed, mBlue, 1, "标准投注", 2));
        mRv.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new ShoppingSSQAdapter(SaveUtils.getGoodsInfos().mlist, this);
        mRv.setAdapter(mAdapter);
        SaveUtils.getGoodsInfos().mTvMoneyNumber += 1;
        SaveUtils.getGoodsInfos().mTvMoney += 2;
        mTvMoneyNumber.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoneyNumber));
        mTvMoney.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoney));
    }

    /**
     * 当用户从双色球界面过来,将用户选择的所有数据展示在当前界面
     *
     * @param event
     */
    private void SetSsqData(SSQDataBean event) {
        if (intType == 1) {
            mTypeSsq.setText("双色球");
            SaveUtils.getGoodsInfos().mlist.add(0, new SSQDataBean(event.getRed(), event.getMblue(), event.getInt(), event.getType(), event.getMoney()));
            setAdapterAndText(event);
        }
    }

    private void setAdapterAndText(SSQDataBean event) {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ShoppingSSQAdapter(SaveUtils.getGoodsInfos().mlist, this);
        mRv.setAdapter(mAdapter);
        SaveUtils.getGoodsInfos().mTvMoneyNumber += event.getInt();
        SaveUtils.getGoodsInfos().mTvMoney += event.getMoney();
        mTvMoneyNumber.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoneyNumber));
        mTvMoney.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoney));
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.tv_add, R.id.iv_return, R.id.tv_next, R.id.select_delete, R.id.zw_tv_bei, R.id.tv_number_bei, R.id.tv_mai})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                if (intType == 1) {
                    startActivity(new Intent(MyApplication.getContext(), SSQSelectActivity.class));
                    finish();
                }
                break;
            case R.id.iv_return:
                if (SaveUtils.getGoodsInfos().mlist.size() == 0) {
                    startActivity(new Intent(MyApplication.getContext(), MainActivity.class));
                } else {
                    initdialog();
                }
                break;
            case R.id.tv_next:
                startActivity(new Intent(MyApplication.getContext(), ZFPayActivity.class));
                finish();
                break;
            case R.id.select_delete:
                if (intType == 1) {
                    SaveUtils.getGoodsInfos().ClearSSQData();
                    mAdapter.notifyDataSetChanged();
                    mTvMoneyNumber.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoneyNumber));
                    mTvMoney.setText(String.valueOf(SaveUtils.getGoodsInfos().mTvMoneyNumber * 2));
                    mTvNext.setBackgroundResource(R.color.f850_default_blue);
                    mTvNext.setClickable(false);
                    mSelectDelete.setBackgroundResource(R.color.f850_default_blue);
                    mSelectDelete.setClickable(false);
                }
                break;

            case R.id.zw_tv_bei:
                initDialog();
                break;
            case R.id.tv_number_bei:
                initDialog();
                break;
            case R.id.tv_mai:
                initDialog();
                break;
        }
    }

    private void initDialog() {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.dalog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.setView(view).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        WindowManager windowManager = this.getWindowManager();
        Display defaultDisplay = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.height = (int) (defaultDisplay.getHeight() * 0.9);
        attributes.width = (int) (defaultDisplay.getWidth() * 0.6);
        window.setAttributes(attributes);


        final TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
        TextView tv_number1 = (TextView) view.findViewById(R.id.tv_number1);
        TextView tv_number2 = (TextView) view.findViewById(R.id.tv_number2);
        TextView tv_number3 = (TextView) view.findViewById(R.id.tv_number3);
        TextView tv_number4 = (TextView) view.findViewById(R.id.tv_number4);
        TextView tv_number5 = (TextView) view.findViewById(R.id.tv_number5);
        TextView tv_number6 = (TextView) view.findViewById(R.id.tv_number6);
        TextView tv_number7 = (TextView) view.findViewById(R.id.tv_number7);
        TextView tv_number8 = (TextView) view.findViewById(R.id.tv_number8);
        TextView tv_number9 = (TextView) view.findViewById(R.id.tv_number9);
        TextView tv_number0 = (TextView) view.findViewById(R.id.tv_number0);
        TextView tv_delete = (TextView) view.findViewById(R.id.tv_delete);
        TextView tv_false = (TextView) view.findViewById(R.id.tv_false);
        TextView tv_true = (TextView) view.findViewById(R.id.tv_true);

        final List<String> mlist = new ArrayList<>();
        mlist.add("1");
        RandomUtils.SetOnClickListener(tv_number1, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number2, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number3, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number4, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number5, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number6, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number7, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number8, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number9, mlist, tv_number);
        RandomUtils.SetOnClickListener(tv_number0, mlist, tv_number);

        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mlist.size() != 0) {
                    mlist.remove(mlist.get(mlist.size() - 1));
                }
                StringBuffer stringBuffer = new StringBuffer();
                for (String s : mlist) {
                    stringBuffer.append(s);
                }
                tv_number.setText(stringBuffer.toString());
            }
        });

        tv_false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        tv_true.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mlist.size() == 0) {
                    mlist.add(String.valueOf(1));
                    RandomUtils.getString(mlist, mTvNumberBei);
                    alertDialog.dismiss();
                    SaveUtils.getGoodsInfos().mTvMoneyNumberBei = 1;
                } else {
                    RandomUtils.getString(mlist, mTvNumberBei);
                    alertDialog.dismiss();
                    String s = mTvMoneyNumber.getText().toString();
                    String s2 = mTvNumberBei.getText().toString();
                    SaveUtils.getGoodsInfos().mTvMoneyNumberBei = Integer.parseInt(s2);
                    int i = Integer.parseInt(s);
                    mTvMoney.setText(i * SaveUtils.getGoodsInfos().mTvMoneyNumberBei * 2 + "");
                }
            }
        });

    }

    /**
     * 当用户在购物车列表中点击返回的时候,提示用户购物车中的数据要清除
     */
    private void initdialog() {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.item_iv_return, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.setView(view).create();
        alertDialog.show();
        view.findViewById(R.id.tv_true).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SaveUtils.getGoodsInfos().ClearSSQData();
                startActivity(new Intent(MyApplication.getContext(), MainActivity.class));
                finish();
            }
        });
        view.findViewById(R.id.tv_false).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        WindowManager windowManager = this.getWindowManager();

        Display defaultDisplay = windowManager.getDefaultDisplay();
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.height = (int) (defaultDisplay.getHeight() * 0.6);
        attributes.width = (int) (defaultDisplay.getWidth() * 0.6);
        window.setAttributes(attributes);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
